package com.apimarket.aop;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * @Description:
 * @Author ZXR
 * @Date 2021/7/9 21:38
 * @Version 1.0
 */

@Slf4j
@Aspect
@Component
public class APIVistTimesAOP {
    /*时间格式*/
    private static String YMDMHS = "yyyy-MM-dd HH:mm:ss";
    /**
     * @Description 切点为请求路径，包含以下请求方式是
     */
    @Pointcut(value =  "@annotation(org.springframework.web.bind.annotation.GetMapping) ||" +
            "@annotation(org.springframework.web.bind.annotation.PostMapping) ||" +
            "@annotation(org.springframework.web.bind.annotation.PutMapping) ||" +
            "@annotation(org.springframework.web.bind.annotation.DeleteMapping) ||" +
            "@annotation(org.springframework.web.bind.annotation.RequestMapping) ")
    public void  flowControl(){
    }

    @Around(value = "flowControl()")
    public Object flowControl(ProceedingJoinPoint joinPoint) throws Throwable {
        Object proceed = null;
        long time = System.currentTimeMillis();
        try {
            proceed = joinPoint.proceed();
            time = System.currentTimeMillis() - time;
            log.info("方法执行消耗时间 = " + time);
            Signature signature = joinPoint.getSignature();
            MethodSignature methodSignature = (MethodSignature) signature;
            Method method = methodSignature.getMethod();
            System.out.println("调用的方法名："+method.getName());
        } catch (Throwable throwable) {
            throw throwable;
        } finally {
            //方法执行后

        }
        return proceed;
    }

}
